package com.danicsoft.daide.command;

import com.danicsoft.daide.token.TokenMap;
import com.danicsoft.daide.token.TokenGroup;

/**
 * Implements the NME command. From Eric Wald's <a href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>, v0.14:
 * <h4><a name="NME">client_command = NME</a> ('name') ('version')</h4>
 * <p>
 * This should be sent by the client to the server as soon as it has connected.
 * It contains the name and version of the client. Both <b>name</b> and
 * <b>version</b> are text fields. They may only contain letters, numbers and
 * underscore. The server will respond with <b>YES (NME ('name') ('version'))</b>,
 * or <b>REJ (NME ('name') ('version'))</b> if the game has already started.
 * </p>
 * <p>
 * The name and version are stored by the server, for storing game records when
 * the game ends. It is not possible for a client to request information about
 * which client is playing each power.
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class NameCommand extends Command {

	/**
	 * The name of this client.
	 */
	public String name;

	/**
	 * The version of this client. This has no impact on anything, and is only
	 * used by the server to track records.
	 */
	public String version;

	/**
	 * Construct a <code>NameCommand</code> from the <code>TokenGroup</code>specified.
	 * 
	 * @param tokens
	 *        A <code>TokenGroup</code> that contains two subgroups: the first
	 *        containing a <code>TextToken</code> with the client's name, and
	 *        the second containing a <code>TextToken</code> containing the
	 *        client's version.
	 * @throws ParseException
	 *         If there was a problem parsing the tokens.
	 */
	public NameCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Construct a new <code>NameCommand</code> for sending to the server.
	 * 
	 * @param name
	 *        A String representing the name of the client.
	 * @param version
	 *        A String representing the version of the client.
	 */
	public NameCommand(String name, String version) {
		this.name = name;
		this.version = version;
	}


	@Override
	public String toString() {
		return "Name:   " + name + "\nVersion: " + version;
	}

	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		name = tokenIO.doText(tokenIO.subGroup(tokens), name);
		version = tokenIO.doText(tokenIO.subGroup(tokens), version);

	}

	@Override
	public byte getType() {
		return TokenMap.Commands.NME;
	}

}
